﻿using SD.App.Desktop.Entity;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.App.Desktop.Repository
{
    public class UserRepository : BaseRepository<User, long>, IUserRepository
    {
        public UserRepository(SqlSugarContext sqlSugarContext, ILogger logger)
            : base(sqlSugarContext, logger)
        {
        }

        /// <summary>
        /// 批量激活用户（特有方法）
        /// </summary>
        public async Task<int> BatchActivateUsersAsync(List<long> userIds)
        {
            // 调用基类的 UpdateByConditionAsync 方法
            return await UpdateByConditionAsync(
                whereExpression: u => userIds.Contains(u.Id),
                updateExpression: u => new User { IsActive = true }
            );
        }

        /// <summary>
        /// 根据用户名查询用户
        /// </summary>
        public async Task<User?> GetUserByUsernameAsync(string username)
        {
            _logger.Debug("查询用户：{Username}", username);
            // SqlSugar 链式查询
            return await _dbClient.Queryable<User>()
                                  .Where(u => u.Username == username)
                                  .FirstAsync();
        }

        /// <summary>
        /// 检查用户名是否已存在
        /// </summary>
        public async Task<bool> UsernameExistsAsync(string username)
        {
            _logger.Debug("检查用户名是否存在：{Username}", username);
            return await _dbClient.Queryable<User>()
                                  .AnyAsync(u => u.Username == username);
        }

    }
}
